﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyCSharpProject
{
    /// <summary>
    /// WindowsServiceOperator.xaml 的交互逻辑
    /// </summary>
    public partial class WindowsServiceOperator : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        private string serviceFilePath;
        private string statusInfo;
        public string StatusInfo
        {
            get { return statusInfo; }
            set { statusInfo = value; OnPropertyChanged("StatusInfo"); }
        }

        private string serviceName;
        public string ServiceName
        {
            get { return serviceName; }
            set { serviceName = value; OnPropertyChanged("ServiceName"); }
        }
        public WindowsServiceOperator()
        {
            InitializeComponent();
            DataContext = this;
            ServiceName = "MyFristService";
            serviceFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZMHWindowsService.exe");

        }
        private void CmdStatus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsServiceExisted(serviceName))
                    using (ServiceController serviceController = new ServiceController(serviceName))
                        StatusInfo = serviceController.Status.ToString();
                else StatusInfo = "Service is not installed！";
            }
            catch (Exception ex) { StatusInfo = $"Error:{ex.Message}\nMaybe the {serviceName} Service is not installed！"; }
        }
        private void CmdUninstall(object sender, RoutedEventArgs e)
        {
            if (IsServiceExisted(serviceName))
            {
                ServiceStop(serviceName);
                UninstallService(serviceFilePath);
            }
        } 
        private void CmdPauseOrContinue(object sender, RoutedEventArgs e)
        {
            ServiceController serviceController = new ServiceController(serviceName);
            if (serviceController.CanPauseAndContinue)
            {

                if (serviceController.Status == ServiceControllerStatus.Running)
                    serviceController.Pause();
                else if (serviceController.Status == ServiceControllerStatus.Paused)
                    serviceController.Continue();
            }
        }

        private void CmdStop(object sender, RoutedEventArgs e)
        {
            if (IsServiceExisted(serviceName)) ServiceStop(serviceName);
        }

        private void CmdStart(object sender, RoutedEventArgs e)
        {
            if (IsServiceExisted(serviceName)) ServiceStart(serviceName);
        }

        private void CmdInstall(object sender, RoutedEventArgs e)
        {
            if (IsServiceExisted(serviceName)) UninstallService(serviceName);
            InstallService(serviceFilePath);
        }
        private bool IsServiceExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        private void InstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }
        }
        private void UninstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
            }
        }
        private void ServiceStart(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }
        }
        private void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }
    }
}
